package com.cee.admin.modules.pm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.pm.entity.PmPolicyDocument;
import com.cee.domain.modules.pm.vo.PmPolicyDocumentVo;
import com.cee.domain.modules.sys.entity.User;
import com.cee.ds.modules.pm.service.IPmPolicyDocumentService;
import com.cee.ds.modules.sys.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cee.admin.common.BaseController;

import java.util.Date;

/**
 * <p>
 * 政策文件 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2024-03-08
 */
@Tag(name = "门户管理-政策文件发布管理")
@RestController
@RequestMapping("/api/pm/policy/document")
public class PmPolicyDocumentController extends BaseController {

    @Autowired
    private IPmPolicyDocumentService pmPolicyDocumentService;

    @Autowired
    private IUserService iUserService;

    @Operation(summary = "分页查询政策文件")
    @GetMapping("/page")
    public Result<Page<PmPolicyDocumentVo>> page(@RequestParam Long current, @RequestParam Long size, PmPolicyDocumentVo query){
        return success(pmPolicyDocumentService.queryPageVo(new Page<>(current, size), query));
    }

    @Operation(summary = "新增政策文件")
    @PostMapping("/save")
    @RequiresPermissions("pm:policy:document:save")
    public Result<Boolean> save(@RequestBody PmPolicyDocument policy){
        policy.setStatus("0");
        return success(pmPolicyDocumentService.saveOrUpdatePolicy(policy));
    }

    @Operation(summary = "修改政策文件")
    @PostMapping("/update")
    @RequiresPermissions("pm:policy:document:update")
    public Result<Boolean> update(@RequestBody PmPolicyDocument policy){
        return success(pmPolicyDocumentService.saveOrUpdatePolicy(policy));
    }

    @Operation(summary = "发布")
    @PostMapping("/publish/{id}")
    @RequiresPermissions("pm:policy:document:publish")
    public Result<Boolean> publish(@PathVariable("id") Long id){
        PmPolicyDocument policy = new PmPolicyDocument();
        policy.setId(id);
        policy.setStatus("1");
        policy.setPublishTime(new Date());
        return success(pmPolicyDocumentService.updateById(policy));
    }

    @Operation(summary = "查看政策文件详情")
    @GetMapping("/get/{id}")
    @RequiresPermissions(value = {"pm:policy:document:get","pm:policy:document:update","pm:policy:document:publish"})
    public Result<PmPolicyDocument> get(@PathVariable("id") Long id){
        PmPolicyDocument policy = pmPolicyDocumentService.getById(id);
        if (policy == null){
            return error("该政策文件不存在");
        }
        User user = iUserService.getById(policy.getCreateUserId());
        PmPolicyDocumentVo policyVo = new PmPolicyDocumentVo();
        BeanUtils.copyProperties(policy, policyVo);
        policyVo.setCreateUserName(user.getName());
        return success(policyVo);
    }

    @Operation(summary = "删除政策文件")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("pm:policy:document:delete")
    public Result<Boolean> delete(@PathVariable("id") Long id){
        return success(pmPolicyDocumentService.removePolicy(id));
    }


}
